/*
 * ChatGroupAction.java
 *
 * Created on 2007年5月15日, 下午3:57
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.ecomm.chat.action;

import com.ecomm.chat.manager.ChatGroupManager;
import com.ecomm.chat.pojo.ChatGroup;
import com.ecomm.platcore.struts.StrutsEntityAction;
import com.ecomm.platcore.struts.support.BusinessException;
import com.ecomm.util.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.commons.lang.StringUtils;
/**
 * 聊天组 信息
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>pijunliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @since 2007年5月15日, 下午3:57
 */
public class ChatGroupAction extends StrutsEntityAction<ChatGroup, ChatGroupManager> {
  
  // 注入 聊天组的 MANAGER
  private ChatGroupManager chatGroupManager;
  /**
   *保存组的信息
   */
  public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
    /*    if (isCancelled(request)) {
            return list(mapping, form, request, response);
        }
        //防止重复提交的token
        saveToken(request);
        if (!isTokenValid(request)) {
            saveDirectlyError(request, "重复提交");
            return mapping.findForward(LIST);
        }
        resetToken(request);
     */
    ChatGroup object = doNewEntity(form, request); //NEW 个对象
    
    String id = request.getParameter(idName);
    try {
      // 将lazyform内容绑定到object
      initEntity(form, request, object);
      if(id != null && !id.equals("")){
        //如果ID存在，那么就更新消息
        chatGroupManager.doUpdateEntity(object);
        object.setId(id);
      }else{
        //如果ID不存在那么就不保存消息
        id =  chatGroupManager.doSaveEntity(object);
        object.setId(id);
      }
      //savedMessage(request, object);
    } catch (BusinessException e) {
      log.error(e.getMessage(), e);
      saveDirectlyError(request, e.getMessage());
      refrenceData(request);
      return mapping.findForward(SUCCESS);
    }
    request.setAttribute("chatGroupobject",object);
    return mapping.findForward(SUCCESS);
  }
  /**
   *通过ID 得到当前组的 对象信息
   */
  public ActionForward find(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
    
    if(request.getParameter(idName)!= null &&  !request.getParameter(idName).equals("00000001")){//表示为新建的
      ChatGroup object = (ChatGroup)doGetEntity(form,request);
      request.setAttribute("chatGroupobject",object);
    }
    return mapping.findForward(SUCCESS);
  }
  /**
   * 删除单个对象的Action函数.
   */
  public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
    try {
      doDeleteEntity(form, request);
      deletedMessage(request);
    } catch (Exception e) {
      saveDirectlyError(request, e.getMessage());
    }
    return null;
  }
  public void setChatGroupManager(ChatGroupManager chatGroupManager) {
    this.chatGroupManager = chatGroupManager;
  }
}
